//
//  AdvertisingView.m
//  iphone.mediacommunity
//
//  Created by wangjun on 11-1-12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "AdvertisingView.h"
#import <QuartzCore/QuartzCore.h>
@implementation AdvertisingView
@synthesize myArray;
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
		[self setBackgroundColor:[UIColor clearColor]];
		if (myAdView==nil) {
			myAdView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 188, 33)];
			[myAdView setClipsToBounds:YES];
			if (myLable==nil) {
				myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];
				[myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
				[myLable setNumberOfLines:0];
				[myLable setBackgroundColor:[UIColor clearColor]];
				[myAdView addSubview:myLable];
			}
			[myAdView setBackgroundColor:[UIColor clearColor]];
			[self addSubview:myAdView];
        }
    }
    return self;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    if (timer!=nil&&[timer isValid]) {
        [timer invalidate];
        timer=nil;
    }
	self.myArray=nil;
	[self.myArray release];
	myAdView=nil;
	[myAdView release];
	myLable=nil;
	[myLable release];
    [super dealloc];
}
-(void)addAdvertisingList
{
    //数据层
    self.myArray=[[NSMutableArray alloc] initWithCapacity:1];
    [self.myArray addObject:@"大家好"];
    [self.myArray addObject:@"We are pleased to announce that the fourth milestone release of the Spring Android project is now available!"];
    [self.myArray addObject:@"Support for Spring Social 1.0.0.RC1, and Spring Security 3.1.0.RC2 through the Spring Android Auth module, which includes a SQLite datastore for persisting OAuth API connections."];
    
    
    if ([self.myArray count]) {
        [myLable setText:@""];
        NSString *text=nil;
        for ( int i=0; i<[self.myArray count]; i++) {
            if (i==0) {
                text=[self.myArray objectAtIndex:i];
            }else{
                text=[NSString stringWithFormat:@"%@\n%@",text,[self.myArray objectAtIndex:i]];
            }
        }
        UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];
        CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
        CGRect rect=myLable.frame;
        rect.size=size;
        [myLable setFrame:rect];
        [myLable setText:text];
        if (timer==nil) {
            timer=[NSTimer scheduledTimerWithTimeInterval: 0.05
                                                   target: self
                                                 selector: @selector(handleTimer:)
                                                 userInfo: nil
                                                  repeats: YES];  
        }
    }
    
}
-(void)handleTimer:(id)sender
{
	if ([self.myArray count]>0) {
		CGRect newFrame1 = myLable.frame;
		if (newFrame1.origin.y<-newFrame1.size.height) {
			newFrame1.origin.y = 23;
			[myLable setFrame:newFrame1];
		}else {
			newFrame1.origin.y =  newFrame1.origin.y-0.8;
			[myLable setFrame:newFrame1];
		}	
	}
}
-(void)drawMainLable:(CGRect)newFrame
{
	CGRect newFrame1 = myLable.frame;
    newFrame1.origin.y =  newFrame1.origin.y+50;
	[myLable setFrame:newFrame1];
}
@end
